/* CustomerController.java
 *
 * Copyright (C) 2013 Universidad de Sevilla
 * 
 * The use of this project is hereby constrained to the conditions of the 
 * TDG Licence, a copy of which you may download from 
 * http://www.tdg-seville.info/License.html
 * 
 */

package controllers.customer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.ActorService;
import services.CommentService;
import services.CustomerService;
import services.PaintingService;
import controllers.AbstractController;
import domain.Comment;
import domain.Customer;
import domain.Painting;

@Controller
@RequestMapping("/comment/customer")
public class CommentCustomerController extends AbstractController {

	//Services -----------------------------------------------------------------
	
	@Autowired
	CommentService commentService;
	
	@Autowired
	CustomerService customerService;

	@Autowired
	PaintingService paintingService;
	
	@Autowired
	ActorService actorService;
	// Constructors -----------------------------------------------------------

	public CommentCustomerController() {
		super();
	}
	

	//Create and Edit
	// Creation ---------------------------------------------------------------

	/*	@RequestMapping(value = "/create", method = RequestMethod.GET)
		public ModelAndView create() {
			ModelAndView result;
			

			Comment comment = new PaintingForm();
			result = createEditModelAndView(paintingForm);
		

			return result;
		}*/

		// Edition ----------------------------------------------------------------

		@RequestMapping(value = "/reply", method = RequestMethod.GET)
		public ModelAndView reply(@RequestParam int commentId) {
			ModelAndView result;
			Comment comment;
			comment = commentService.findOne(commentId);				
			result = new ModelAndView("comment/list");
			Comment newComment = commentService.create(comment);
			result.addObject("comment", newComment);
			result.addObject("comments", comment.getChildren());
			result.addObject("URI", "/comment/customer/reply.do");
			return result;
		}
		@RequestMapping(value = "/goParent", method = RequestMethod.GET)
		public ModelAndView goParent(@RequestParam int commentId) {
			ModelAndView result;
			Comment comment;
			comment = commentService.findOne(commentId);				
			result = new ModelAndView("comment/list");
			if(comment.getParent()!=null){
			Comment newComment = commentService.create(comment.getParent());
			result.addObject("comment", newComment);
			result.addObject("comments", comment.getParent().getChildren());
			result.addObject("URI", "/comment/customer/goParent.do");
			}
			else
			{
				result.addObject("comment", commentService.create(comment.getPainting()));
				result.addObject("comments", commentService.findCommentParentByPaintingId(comment.getPainting().getId()));
					
			}
			return result;
		}
		
	@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
	public ModelAndView save(@Valid Comment comment, BindingResult bindingResult) {
		ModelAndView result;		


		if (bindingResult.hasErrors()) {
			result = createEditModelAndView(comment);

		} else {
			try {
				
				Customer customer = comment.getCustomer();
				
				Collection<Comment> c = customer.getComments();
				List<Comment> l1 = new ArrayList<Comment>();
				l1.addAll(c);
				l1.add(comment);
				customer.setComments(l1);
				customerService.addComment(customer);
				
				Painting painting = comment.getPainting();
				
				Collection<Comment> p = painting.getComments();
				List<Comment> l2 = new ArrayList<Comment>();
				l2.addAll(p);
				l2.add(comment);
				painting.setComments(l2);
				paintingService.modify(painting);
				
				commentService.save(comment);
				if(comment.getParent()==null){
				result = new ModelAndView("redirect:/comment/list.do?paintingId="+comment.getPainting().getId());
				}
				else{
					result =new ModelAndView("redirect:/comment/customer/reply.do?commentId="+comment.getParent().getId());
				}
				result.addObject("message", "painting.commit.success");
			} catch (Throwable oops) {
				result = createEditModelAndView(comment, oops.getMessage());
			}
		}

		return result;
	}


	// Ancillary methods ------------------------------------------------------

	protected ModelAndView createEditModelAndView(Comment comment) {
		ModelAndView result;

		result = createEditModelAndView(comment, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(Comment comment, String message) {
		ModelAndView result;


		result = new ModelAndView("comment/list");
		result.addObject("comment", comment);
		result.addObject("message", message);
		result.addObject("comments", commentService.findCommentParentByPaintingId(comment.getPainting().getId()));
		result.addObject("URI", "/comment/customer/edit.do");

		return result;
	}
	
//List
	
	
	
}